package server.business.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.FullHttpResponse;

import java.util.Map;

import server.business.entity.ResultMessage;
import server.business.entity.User;
import server.business.service.HttpServiceFactory;
import server.business.service.IHttpService;

import com.alibaba.fastjson.JSON;
import com.core.rest.AbstractHttpHandler;


public class UserQueryByNameHandler extends AbstractHttpHandler implements Cloneable{

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    @Override
    protected void getData(Map<String, String> params, ChannelHandlerContext ctx) {


        String text = "";

        IHttpService service = HttpServiceFactory.getService();

        ResultMessage rm = new ResultMessage(true);
        
        String userName = "";

        if (params == null || params.isEmpty()) {

            rm.setSuccess(false);
            
            rm.setMessage("please check your params");
            
        } else {

            userName = params.get("userName");

            if (userName == null || userName.trim().equals("")) {

                text = "please send an valid user name to server";
            }
        }
        
        User user = service.getUserByName(userName);
        
        rm.setMessage(user);

        text = JSON.toJSONString(rm, true);

        FullHttpResponse response = wrapContent(text);

        sendResponse(ctx, response);
    
        
    }

    @Override
    protected void postData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void deleteData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    protected void updateData(Map<String, String> params, ChannelHandlerContext ctx) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public AbstractHttpHandler deepCopy() {
        
        AbstractHttpHandler httpHandler = null;
        
        try {
            
            httpHandler = (AbstractHttpHandler)(this.clone());
        
        } catch (CloneNotSupportedException e) {
            
            e.printStackTrace();
        }
        
        return httpHandler;
    }
    
    
}
